import requests
import json

# 需要填上你自己的cookie，否则403
cookie = 'gk_user_auth=41e3ee7db340466e9191a042e0e5e680'

headers = {
  'Host': 'prop.gorkor.com',
  'accept': 'application/json, text/plain, */*',
  'origin': 'https://prop.gorkor.com',
  'x-requested-with': 'XMLHttpRequest',
  'user-agent': 'Mozilla/5.0 (Linux; Android 5.1.1; vmos Build/LMY48G; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/52.0.2743.100 Mobile Safari/537.36',
  'client-type': '0',
  'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  'accept-encoding': 'gzip, deflate',
  'accept-language': 'zh-CN,en-US;q=0.8',
  'referer': 'https://prop.gorkor.com/dailyquestion'
}

def getHeaders():
    return headers

def set_cookie(input_cookie):
    headers['cookie'] = input_cookie

def getResult(resp):
    if resp.status_code != 200:
        return False, resp
    print("resp : {}".format(resp))
    resp = json.loads(resp.text)
    # print('getResult ---- resp : {}'.format(resp))
    if resp['message'] == 'Success':
        return True, resp['result']
    else:
        return False, resp

def getQuestionId():    
    payload = "data=%7B%22start%22%3A0%2C%22count%22%3A1%7D"
    url = "https://prop.gorkor.com/api/prop/dailyquestion/getQuestionList?r=0.34386511751254"
    set_cookie(cookie)
    success,result = getResult(requests.request("POST", url, headers=headers, data=payload))
    if success:
            print('已连续签到天数 : 【{}】'.format(result['days']))
            first_question = result['answers'][0]
            print(first_question)
            if ('answer' not in first_question):
                return result['answers'][0]['id']
            else:
                raise Exception('今日已签到！')
    else:
         raise Exception("请求失败：【{}】".format(result))

def choiceYes(id):
    url = "https://prop.gorkor.com/api/prop/dailyquestion/answerQuestion?r=0.059194135685852745"
    payload = "data=%7B%22questionId%22%3A{}%2C%22answer%22%3A1%7D"

    success, result = getResult(requests.request("POST", url, headers=headers, data=payload.format(id)))
    if success:
        print('表态成功')
        return True
    else:
        raise Exception("表态失败：【{}】".format(result))

def postComment(id):
    url = "https://prop.gorkor.com/api/prop/dailyquestion/postComment?r=0.07553658512035422"
    payload = "data=%7B%22questionId%22%3A%22{}%22%2C%22comment%22%3A%22%E6%B0%B4%22%2C%22pid%22%3A-1%7D"
    success, result = getResult(requests.request("POST", url, headers=headers, data=payload.format(id)))
    if success:
        print('评论成功！')
    else:
        raise Exception('评论失败: 【{}】'.format(result))

def sign():    
    url = "https://prop.gorkor.com/api/prop/dailyquestion/pointReceive?r=0.1489448494609289"
    payload = "https://prop.gorkor.com/api/prop/dailyquestion/pointReceive?r=0.1489448494609289"
    success, result = getResult(requests.request("POST", url, headers=headers, data=payload))
    if success:
        print('签到成功！')
    else:
        raise Exception('签到失败: 【{}】'.format(result))



def auto():
    id = getQuestionId()
    # 表态
    choiceYes(id)
    # 评论
    postComment(id)(id)
    # 签到
    sign()
    getQuestionId()

auto()